package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.pojo.MyCollection;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @ClassName CommentServiceImpl
 * @Author RC
 * @date 2021.01.05 10:46
 */
@Component
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public PageBean<MyCollection> getComment(String goodsid, String commentlevel, Integer pageNumber, Integer pageSize) {
        // 页码
        if (pageNumber == null || pageNumber < 0) {
            pageNumber = 1;
        }
        // 页数
        if (pageSize == null || pageSize < 1) {
            pageSize = 1;
        }
        PageBean<MyCollection> page = new PageBean<>();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);

        List<MyCollection> list = new ArrayList<>();
        int counts = 0;
       if("".equals(commentlevel ) || commentlevel ==null){
            Query query = new Query(Criteria.where("goodsid").is(goodsid));
            query.skip((page.getStartIndex())).limit(pageSize);
            list =  mongoTemplate.find(query, MyCollection.class, "mycollection");
            Long count = mongoTemplate.count(query, MyCollection.class);
            counts = count.intValue();
       }else {
            Query query = new Query();
            query.addCriteria(Criteria.where("goodsid").is(goodsid));
            query.addCriteria(Criteria.where("commentlevel").is(commentlevel));
            query.skip((page.getStartIndex())).limit(pageSize);
            list = mongoTemplate.find(query, MyCollection.class, "mycollection");
            Long count = mongoTemplate.count(query, MyCollection.class);
            counts = count.intValue();
        }
        page.setTotalCount(counts);
        page.setResult(list);
        return page;
    }

    @Override
    public void saveComment(MyCollection myCollection) {
        myCollection.setCreatedatetime(new Date());
        myCollection.setLikenum("200");
        myCollection.setImages("https://firstcontent.oss-cn-beijing.aliyuncs.com/dongdongshopc_0029.jpg");
        mongoTemplate.save(myCollection,"mycollection");

    }
}

